from API.crypto_news import (get_aicoin_bulletin_by_last_id,
                             get_aicoin_bulletin_last_id,
                             get_jinse_bulletin_by_id,
                             get_social_metrics_list,
                             get_indicators_via_http)
import os
import pandas as pd
import datetime


DATA_DIR = 'E:\\data'

def update_social_metric(asset='bitcoin', metric=None, start_date=None, end_date=None):
    # 更新单个舆情指标数据
    print(f'获取{metric}数据')
    if end_date is None:
        end_date = datetime.datetime.now().strftime('%Y-%m-%d')
    folder_name = os.path.join(DATA_DIR, f'social_data\\sanbase')
    os.makedirs(folder_name, exist_ok=True)

    file_name = os.path.join(folder_name, f'{metric}.csv')
    if os.path.exists(file_name):
        historical_df = pd.read_csv(file_name, index_col='end_date')
        start_date = historical_df.index[-1]
        df = get_indicators_via_http(indic_name=metric, asset=asset, start_date=start_date, end_date=end_date)
        if end_date in df.index:
            df.drop(index=end_date, inplace=True)
        if start_date == df.index.max():
            return
        df = pd.concat([historical_df.iloc[:-1, ], df.loc[historical_df.index[-1]:, ]])
        df.sort_index(inplace=True)
        df.to_csv(file_name)
    else:
        df = get_indicators_via_http(indic_name=metric, asset=asset, start_date=start_date, end_date=end_date)
        if end_date in df.index:
            df.drop(index=end_date, inplace=True)
        if not df[df[f'{metric}'] != 0].empty:
            df = df.loc[df[df[f'{metric}'] != 0].index[0]:, :]
        df.sort_index(inplace=True)
        df.to_csv(file_name)



# 更新BTC舆情指标数据
print(f'获取BTC舆情统计数据')
social_metrics = get_social_metrics_list(asset='bitcoin')
for metric in social_metrics:
    update_social_metric(asset='bitcoin', metric=metric, start_date='2015-01-01', end_date=None)